package com.mhkj.supervise.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.revise.plan.entity.PlanNode;
import com.mhkj.revise.plan.service.PlanNodeService;
import com.mhkj.supervise.entity.SuperviseProblem;
import com.mhkj.supervise.entity.SuperviseProblemScheme;
import com.mhkj.supervise.service.SuperviseProblemSchemeService;
import com.mhkj.supervise.service.SuperviseProblemService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.common.exception.ErrorCode;
import com.mics.core.common.util.ftp.FtpUtil;
import com.mics.core.system.base.entity.FileEntity;
import com.mics.core.system.base.service.FileService;
import com.mics.core.system.organizational.bean.Employee;
import com.mics.core.system.organizational.service.OrgService;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 * 督导问题
 */

@Controller
@RequestMapping("/supervise/problem")
public class SuperviseProblemController {
	 
	@Autowired
	private SuperviseProblemService superviseProblemService;
	@Autowired
	private SuperviseProblemSchemeService superviseProblemSchemeService;
	@Autowired
	private OrgService orgService;
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private PlanNodeService planNodeService;
	@Autowired
	private FileService fileService;
	/**
	 * 分页查询督导问题
	 * 
	 */
	@RequestMapping(value = "/listByPage", method = RequestMethod.POST)
	@ResponseBody
	public Pagination<SuperviseProblem> listByPage(SuperviseProblem SuperviseProblem, int pageIndex, int pageSize) {

		Pagination<SuperviseProblem> pagination = superviseProblemService.listByPage(SuperviseProblem, pageIndex,
				pageSize);
		List<SuperviseProblem> data = pagination.getData();
		for (SuperviseProblem ssp : data) {
			String createBy = ssp.getCreateBy();
			Employee employeeInfo = orgService.employeeInfo(createBy);
			ssp.setCreateName(employeeInfo.getName());
		}

		return pagination;
	}

	/**
	 * 获取督导问题记录分页列表
	 * 
	 * @throws Exception
	 * 
	 */
	@RequestMapping(value = "/listRecordByPage", method = RequestMethod.POST)
	@ResponseBody
	public Pagination<SuperviseProblem> listRecordByPage(SuperviseProblem SuperviseProblem, int pageIndex, int pageSize)
			throws Exception {

		Pagination<SuperviseProblem> pagination = superviseProblemService.listRecordByPage(SuperviseProblem, pageIndex,
				pageSize);
		List<SuperviseProblem> data = pagination.getData();
		for (SuperviseProblem ssp : data) {
			String createBy = ssp.getCreateBy();
			Employee employeeInfo = orgService.employeeInfo(createBy);
			ssp.setCreateName(employeeInfo.getName());

			PlanNode planNode = ssp.getPlanNode();
			if(planNode.getPlan().getItem()!=null) {
				ssp.setItemName(planNode.getPlan().getItem().getName());
			}
			ssp.setEndTime(planNode.getEndDate());
			ssp.setStartTime(planNode.getBeginDate());
			ssp.setPlanType(planNode.getPlan().getPlanType().getValue());
			ssp.setPlanState(planNode.getState());
			ssp.setPlanName(planNode.getNodeName());
			String postId = planNode.getPostId();
			if (orgService.getEmployeeByPost(postId).size() > 0) {
				ssp.setExeName(orgService.getEmployeeByPost(postId).get(0).getName());
			}
		}
		return pagination;
	}

	/**
	 * 保存
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage save(String problem, Long planNodeId, String scheme, Integer status,String fileUrl,String fileName,String remark) {

		SuperviseProblem superviseProblem = new SuperviseProblem();
		if (status == 1) {
			superviseProblem.setStatus(status);
		}
		PlanNode planNode = planNodeService.getById(planNodeId);
		superviseProblem.setPlanNode(planNode);
		superviseProblem.setProblem(problem);
		superviseProblem.setRemark(remark);
		String message = superviseProblemService.save(superviseProblem);
		
		
		// 保存附件
		if (fileName != null && !fileName.isEmpty()) {
			String[] fileNameArr = fileName.split(";");
			String[] fileUrlArr = fileUrl.split(";");

			// 循环插入数据库
			for (int i = 0; i < fileNameArr.length; i++) {
				FileEntity fileEntity = new FileEntity();
				fileEntity.setTableId(superviseProblem.getId());
				fileEntity.setTableName("t_supervise_problem");
				fileEntity.setFileName(fileNameArr[i]);
				fileEntity.setFileUrl(fileUrlArr[i]);
				fileService.save(fileEntity);
			}
		}
		 
		
		if (superviseProblem.getId() == null) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "保存督导问题失败!");
		}
		
		if(!StringUtils.isEmpty(scheme)) {
			String[] split = scheme.split("@_@");
			for (String str : split) {
				SuperviseProblemScheme spscheme = new SuperviseProblemScheme();
				spscheme.setScheme(str);
				spscheme.setSuperviseProblem(superviseProblem);
				superviseProblemSchemeService.save(spscheme);
			}
		}

		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 修改
	 * 
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage edit(String problem, Long id, String scheme, String delIds, String editIds, Integer status,String  newScheme,String fileUrl,String fileName,String remark) {

		SuperviseProblem superviseProblem = superviseProblemService.getById(id);
		if (status == 1) {
			superviseProblem.setStatus(status);
		}
		superviseProblem.setProblem(problem);
		superviseProblem.setRemark(remark);
		String message = superviseProblemService.save(superviseProblem);
		
		
		
		
		// 保存附件
		if (fileName != null && !fileName.isEmpty()) {
			String[] fileNameArr = fileName.split(";");
			String[] fileUrlArr = fileUrl.split(";");

			// 循环插入数据库
			for (int i = 0; i < fileNameArr.length; i++) {
				FileEntity fileEntity = new FileEntity();
				fileEntity.setTableId(superviseProblem.getId());
				fileEntity.setTableName("t_supervise_problem");
				fileEntity.setFileName(fileNameArr[i]);
				fileEntity.setFileUrl(fileUrlArr[i]);
				fileService.save(fileEntity);
			}
		}
		
		
		
		
		if (superviseProblem.getId() == null) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "修改督导问题失败!");
		}
		if(!StringUtils.isEmpty(newScheme)) {
		String[] newsplit = newScheme.split("@_@");
		for (String str : newsplit) {
			SuperviseProblemScheme newspscheme = new SuperviseProblemScheme();
			newspscheme.setScheme(str);
			newspscheme.setSuperviseProblem(superviseProblem);
			superviseProblemSchemeService.save(newspscheme);
		}
		}
		if (!StringUtils.isEmpty(editIds) && !StringUtils.isEmpty(scheme)) {
			String[] edids = editIds.split(",");
			String[] split = scheme.split("@_@");
			if (edids.length != split.length) {
				return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "解决方案与解决方案id个数不同,修改失败!");
			}

			for (int i = 0; i < split.length; i++) {

				String string = edids[i];
				long parseLong = Long.parseLong(string);
				SuperviseProblemScheme sps = superviseProblemSchemeService.getById(parseLong);
				sps.setScheme(split[i]);
				superviseProblemSchemeService.edit(sps);
			}

		}

		if (!StringUtils.isEmpty(delIds)) {
			String[] spdel = delIds.split(",");
			for (String st : spdel) {
				long parseLong = Long.parseLong(st);
				SuperviseProblemScheme byId = superviseProblemSchemeService.getById(parseLong);
				superviseProblemSchemeService.del(byId);
			}
		}

		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/del", method = { RequestMethod.POST })
	@ResponseBody
	public ResponseMessage del(Long id) {
		if (StringUtils.isEmpty(id)) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "id为空!");
		}
		List<SuperviseProblemScheme> list = superviseProblemSchemeService.findAllByProblemId(id);
		if (list.size() > 0) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "存在子数据,无法删除!");
		}

		SuperviseProblem SuperviseProblem = new SuperviseProblem();
		SuperviseProblem.setId(id);
		String message = superviseProblemService.del(SuperviseProblem);
		
		List<FileEntity> listFileByTableIdAndName = fileService.listFileByTableIdAndName(id, "t_supervise_problem");
		for (FileEntity fileEntity : listFileByTableIdAndName) {
			 fileService.del(fileEntity);
			try {
				FtpUtil.deleteFile(fileEntity.getFileUrl());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 查询督导问题信息
	 */
	@RequestMapping(value = "/info", method = RequestMethod.POST)
	@ResponseBody
	public SuperviseProblem info(@RequestParam(value = "id", required = true) long id) {
		SuperviseProblem SuperviseProblem = superviseProblemService.getById(id);
		List<SuperviseProblemScheme> list = superviseProblemSchemeService.findAllByProblemId(id);
		SuperviseProblem.setListsp(list);
		PlanNode planNode = SuperviseProblem.getPlanNode();
		SuperviseProblem.setPlanState(planNode.getState());
		SuperviseProblem.setPlanName(planNode.getNodeName());
		SuperviseProblem.setRiskState(planNode.getRiskState());
		
		String postId = planNode.getPostId();
		if (orgService.getEmployeeByPost(postId).size() > 0) {
			SuperviseProblem.setExeName(orgService.getEmployeeByPost(postId).get(0).getName());
		}
		//设置附件
		List<FileEntity> listFileByTableIdAndName = fileService.listFileByTableIdAndName(id, "t_supervise_problem");
		SuperviseProblem.setListFile(listFileByTableIdAndName);
		
		
		return SuperviseProblem;
	}

}
